package com.xia.netty.thread.synchronize;

public class SynchronizedObjectLock1 implements Runnable {
    static SynchronizedObjectLock1 synchronizedObjectLock = new SynchronizedObjectLock1();
    static SynchronizedObjectLock1 synchronizedObjectLock2 = new SynchronizedObjectLock1();

    @Override
    public void run() {
        method();
    }

    /**
     * synchronized 用在普通方法上默认锁当前实例,用在静态方法上默认锁方法所在的类,只有一把锁
     */
    private static synchronized void method() {
        String name = Thread.currentThread().getName();
        System.out.println("线程: " + name);
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("线程:" + name + "结束");
    }

    public static void main(String[] args) {
        Thread thread1 = new Thread(synchronizedObjectLock);
        Thread thread2 = new Thread(synchronizedObjectLock2);
        thread1.start();
        thread2.start();
    }
}
